function processData(data: FormData, result: FormData[][]): FormData[][] {

  let found: boolean = false;
  console.log(result)

  result.some((subArray, subIndex) => {
    let matched = subArray.some((item, index) => {
      // 如果名称相同
      if (item.exerciseName === data.exerciseName) {
        console.log("名称相同");
        found = true
        // 如果重量相同
        console.log(item.weight, data.weight)
        if (item.weight === data.weight && item.unit === data.unit) {
          console.log("重量相同");
          // 如果每组的个数相同
          if (item.per === data.per) {
            console.log("个数相同");
            // 组数合并
            item.group += data.group;
            return true;
          } else {
            console.log("个数不同");
            subArray.push(data);
            return true;
          }
        } else {
          console.log("重量不同");
          subArray.push(data);
          return true;
        }
      } else {
        console.log("名称不同");
        console.log(subIndex, result)
        if (subIndex === result.length - 1) {
          return true;
        }
      }
    })
    if (!matched && subIndex === result.length - 1) {
      subArray.push(data);
    }
    return matched
  })
  if (!found) {
    result.push([data]);
  }
  return result;
}

function editData(data: FormData[][], editData: EditData): FormData[][] {
  const temp = {
    weight: editData.weight,
    group: 1,
    per: editData.per,
    exerciseName: editData.exercisename,
    unit: editData.unit,
    exerciseAreaId: editData.exerciseareaid,
    exerciseMoveId: editData.exercisemoveid
  }
  // 先将之前的减一
  data[editData.list][editData.group].group -= 1;
  if(data[editData.list][editData.group].group === 0) {
    data[editData.list].splice(editData.group, 1);
  }
  data[editData.list].some((item, index) => {
    if (item.weight === editData.weight && item.unit === editData.unit) {
      if (item.per === editData.per) {
        item.group += 1;
        return true;
      } else {
        if (index === data[editData.list].length - 1) {
          data[editData.list].push(temp)
          return true;
        }
      }
    } else {
      if (index === data[editData.list].length - 1) {
        data[editData.list].push(temp)
        return true;
      }
    }
  })

  return data;
}

module.exports = {
  processData,
  editData
}
